//
//  modelText.swift
//  GitLearn
//
//  Created by Qingke on 16/4/6.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit
class Beauty:NSObject,NSCoding{
    var name: String
    var photo: UIImage?
    var rating: Int
    
    static let documentDirectory = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
    static let archiveURL = documentDirectory.URLByAppendingPathComponent("beauty")                                       //static类的变量全局都可以用

    init?(name:String,photo:UIImage?,rating:Int) {
        self.name = name
        self.photo = photo
        self.rating = rating
        
        super.init()
        
        if rating < 0 || name.isEmpty {
            return nil
        }
    }
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(name,forKey: "name")
        aCoder.encodeObject(photo,forKey: "photo")
        aCoder.encodeInteger(rating, forKey: "rating")
        
    }
    required convenience init?(coder aDecoder: NSCoder) {
        let name = aDecoder.decodeObjectForKey("name") as! String
        let photo =  aDecoder.decodeObjectForKey("photo") as? UIImage
        let rating = aDecoder.decodeIntegerForKey("rating")
        
        self.init(name: name,photo: photo,rating: rating)
    }
    
}
